// [Timers](timers) are for when you want to do
// something once in the future - _tickers_ are for when
// you want to do something repeatedly at regular
// intervals. Here's an version1 of a ticker that ticks
// periodically until we stop it.

package main

import (
	"fmt"
	"time"
)

// func main() {
//
//		// Tickers use a similar mechanism to timers: a
//		// channel that is sent values. Here we'll use the
//		// `select` builtin on the channel to await the
//		// values as they arrive every 500ms.
//		ticker := time.NewTicker(500 * time.Millisecond)
//		done := make(chan bool)
//
//		go func() {
//			for {
//				select {
//				case <-done:
//					return
//				case t := <-ticker.C:
//					fmt.Println("Tick at", t)
//				}
//			}
//		}()
//
//		// Tickers can be stopped like timers. Once a ticker
//		// is stopped it won't receive any more values on its
//		// channel. We'll stop ours after 1600ms.
//		time.Sleep(1600 * time.Millisecond)
//		ticker.Stop()
//		done <- true
//		fmt.Println("Ticker stopped")
//	}
func main() {
	// 创建一个定时器，每隔1秒触发一次
	ticker := time.NewTicker(1 * time.Second)

	defer ticker.Stop() // 在程序退出时停止定时器，释放相关资源

	// 在goroutine中监听定时器的触发事件
	go func() {
		for {
			select {
			case <-ticker.C:
				// 定时器触发时执行的任务
				fmt.Println("Ticker ticked!")
			}
		}
	}()

	// 主程序等待一段时间，以便观察定时器的触发
	time.Sleep(5 * time.Second)

}
